<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>我爱电影网</title>
    <script src="./data/jq.js"></script>
    <link rel="stylesheet" href="./data/bootstrap.min.css">
    <script src="./data/bootstarp.min.js"></script>
    <link rel="stylesheet" href="./data/pagination.css">
    <script src="./data/pagination.js"></script>
    <link rel="stylesheet" href="./css/5-2022-11-4-lidare.css">
</head>

<body>
    <nav class="navbar navbar-default movieNav">
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand home-my" href="#">我爱电影网</a>
            </div>
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li class="home-my active"><a href="#">首页 <span class="sr-only">(current)</span></a></li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                            aria-expanded="false">分类 <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <li><a href="#">科幻</a></li>
                            <li><a href="#">动作</a></li>
                            <li><a href="#">娱乐</a></li>
                            <li><a href="#">恐怖</a></li>
                            <li><a href="#">悬疑</a></li>
                            <li><a href="#">剧情</a></li>
                        </ul>
                    </li>
                </ul>
                <div class="navbar-form navbar-left form-my">
                    <div class="form-group">
                        <input type="text" class="form-control ipt-my" placeholder="杀手">
                    </div>
                    <button type="button" class="btn btn-default btn-search">搜索</button>
                </div>
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#">注册</a></li>
                    <li><a href="#">登录</a></li>
                </ul>
            </div>
        </div>
    </nav>
    <div class="container">
        <div class="jumbotron header-my showBig">
            <div class="mecha"></div>
            <div class="titleBox">
                <!-- 插槽 -->
            </div>
            <p>我爱电影网，涵盖最新电影、好看的电影、经典电影、电影推荐、免费电影、高清电影在线观看及海量最新电影图文视频资讯，找资源就上我爱电影网。</p>
            <p><a class="btn btn-primary btn-lg bgc upZ showMore" href="#" role="button"
                    style="width: 82px;height:34px;font-size: 14px;display: flex;justify-content: center;align-items: center;">查看更多</a>
            </p>
        </div>
    </div>
    <hr>
    <div class="container">
        <div class="row padd">
            <div class="hisSearch">历史搜索</div>
            <div class="historyBox">
                <!-- 插槽 -->
            </div>
        </div>
        <div class="row padd hisCallSearch"></div>
    </div>
    <hr>
    <div class="container">
        <div class="row padd">
            <div class="col-lg-8 col-md-8 col-sm-6">
                <div class="row movie-list">
                    <!-- 插槽 -->
                </div>
                <div class="row">
                    <div id="Pagination" class="pagination"></div>
                </div>
            </div>
            <div class="col-lg-4 col-md-4 col-sm-6 news-all">
                <div class="new">
                    <div class="new-title newAll">最新电影</div>
                    <div class="new-content newMovie">
                        <!-- 插槽 -->
                    </div>
                </div>
                <div class="new">
                    <div class="new-title hotAll">热门电影</div>
                    <div class="new-content hotMovie">
                        <!-- 插槽 -->
                    </div>
                </div>
            </div>
        </div>
    </div>
    <hr>
    <div class="container">
        <div class="footer">
            <p>CopyRight © 2022 我爱电影网</p>
            <p>专注于电影资源的传播 <a href="#">粤ICP备xx21114号</a></p>
        </div>
    </div>
    <script src="./data/5-2022-11-5-lidare.js"></script>
    <script>
        jQuery(function () {
            callHistory();
            callResize();
            callLaypage();
            createTitle();
            callList({
                type: "get",
                url: "https://api.52kfw.cn/api/v1/movie/top",
                page: 1,
                size: 10,
                parent: ".newMovie"
            });
            callList({
                type: "get",
                url: "https://api.52kfw.cn/api/v1/movie/hot",
                page: 1,
                size: 10,
                parent: ".hotMovie"
            });
        });
        function callHistory() {
            let url = "https://api.52kfw.cn/api/v1/movie/list?kw=";
            let cat = new Set();
            let time = null;
            let intercept = false;
            jQuery(".home-my").on({
                click: function () {
                    jQuery(".hisCallSearch").html(``)
                    getMovieList();
                }
            });
            jQuery(".ipt-my").on({
                keydown: function (e) {
                    if (e.keyCode === 13) {
                        jQuery(".btn-search").trigger("click");
                    }
                }
            });
            jQuery(".btn-search").on({
                click: function () {
                    if (intercept) return;
                    intercept = true;
                    setTimeout(() => intercept = false, 500);
                    jQuery(".ipt-my")[0].value = jQuery(".ipt-my")[0].value ? jQuery(".ipt-my")[0].value : jQuery(".ipt-my")[0].placeholder;
                    setCat(jQuery(".ipt-my")[0].value);
                    if (jQuery(".ipt-my")[0].value == "") return;
                    getMovieList(url + jQuery(".ipt-my")[0].value);
                },
            });
            jQuery(".historyBox").on({
                click: function (e) {
                    getMovieList(url + this.textContent.slice(0, -1));
                    setCat(this.textContent.slice(0, -1));
                }
            }, ".historyItem");
            jQuery(".historyBox").on({
                click: function (e) {
                    e.stopPropagation();
                    setCat(jQuery(this).parents(".historyItem").text().slice(0, -1), true);
                }
            }, ".delete-my")
            function setCat(value, dele = false) {
                if (value == "") return;
                if (cat.has(value) || dele) cat.delete(value);
                if (!dele) cat.add(value);
                jQuery(".historyBox").html("");
                for (let value of cat) {
                    jQuery(".historyBox").prepend(`<div class="historyItem">${value}<div class="delete-my">x</div></div>`);
                };
                if (dele) return;
                jQuery(".hisCallSearch").html(`[${value}]的搜索结果`);
            }
        }
        function callResize() {
            window.onresize = function () {
                if (window.innerWidth <= 767) {
                    jQuery(".item").css({
                        marginRight: "0",
                    });
                } else {
                    jQuery(".item").css({
                        marginRight: "10px"
                    });
                };
            }
        }
        //请求函数
        function callList(options) {
            jQuery.ajax({
                type: options.type,
                data: {
                    page: options.page,
                    size: options.size
                },
                url: options.url,
                success: function (result) {
                    let { result: { movieList } } = result;
                    for (let i = 0; i < movieList.length; i++) {
                        let { createdAt, movieName } = movieList[i];
                        jQuery(`
                            <div class="new-item">
                                <a href="#">${movieName}</a>
                                <span>${createdAt}</span>
                            </div>
                        `).appendTo(options.parent);
                    };
                }
            });
        };
        function callLaypage() {
            let size = 3;
            let page = 1;
            let url = "https://api.52kfw.cn/api/v1/movie/list";
            jQuery.ajax({
                type: "get",
                data: {
                    page,
                    size
                },
                url,
                success: function (result) {
                    let { result: { total } } = result;
                    let initPagination = function () {
                        // 创建分页
                        $("#Pagination").pagination(total, {
                            num_edge_entries: 1, //边缘页数
                            num_display_entries: 4, //主体页数
                            callback: pageselectCallback,
                            items_per_page: size, //每页显示1项
                            prev_text: "前一页",
                            next_text: "后一页"
                        });
                    };
                    function pageselectCallback(page_index, jq) {
                        getMovieList(url, page_index + 1, size);
                        return false;
                    }
                    initPagination();
                }
            });
        }
        function getMovieList(url = "https://api.52kfw.cn/api/v1/movie/list", page = 1, size = 3) {
            jQuery.ajax({
                type: "get",
                data: {
                    page,
                    size
                },
                url,
                success: function (result) {
                    let { result: { movieList } } = result;
                    //分页
                    jQuery(".movie-list").html("");
                    for (let i = 0; i < movieList.length; i++) {
                        let { actors, createdAt, movieName, director, pv, movieImg } = movieList[i];
                        jQuery(`
                            <div class="col-lg-4 col-md-6 mgpd-0">
                                <div class="item">
                                    <div class="item-img">
                                        <img src="https://api.52kfw.cn${movieImg}" alt="">
                                    </div>
                                    <div class="item-name">${movieName}</div>
                                    <div class="item-time">发布时间:<span>${createdAt}</span></div>
                                    <div class="item-actor">演员:<span>${actors.length > 6 ? actors.slice(0, 6) + "..." : actors}</span></div>
                                    <div class="item-director">导演:<span>${director.length > 6 ? director.slice(0, 6) + "..." : director}</span></div>
                                    <div class="item-views">浏览量:<span>${pv}</span></div>
                                    <div class="item-details bgc">详情</div>
                                </div>
                            </div>
                        `).appendTo(".movie-list");
                    }
                }
            });
        }
        function createTitle() {
            jQuery(".titleBox").html(svg);
            jQuery(".showMore").on({
                click: function () {
                    jQuery(".titleBox").html("").html(svg);
                }
            });
        }
    </script>
</body>

</html>